package com.uw.gateway.handler.router;


import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class HelloRouterHandler {

    public Mono<ServerResponse> handle(ServerRequest request) {

        String requestParam = """
                {
                	"args": [
                		[
                			{
                				"id": 11,
                				"name": "uw"
                			}
                		]
                	],
                	"methodSign": "listByUser(java.util.List)",
                	"parameters": {},
                	"service": "com.uw.uwrpc.demo.api.UserService"
                }
                """;

        WebClient webClient = WebClient.create("http://10.229.37.120:7070/uwrpc");
        Mono<ResponseEntity<String>> entity = webClient.post()
                .header("Content-Type", "application/json")
                .bodyValue(requestParam)
                .retrieve()
                .toEntity(String.class);

        Mono<String> body = entity.map(ResponseEntity::getBody);
        body.subscribe(source -> System.out.println("response:" + source));

        return ServerResponse.ok()
                .header("Content-Type", "application/json")
                .header("uw.gw.version", "v1.0.0")
                .body(body, String.class);
    }

}
